<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">
    <div class="article-intro">

        <h1>Java StringBuffer 和 StringBuilder 类</h1>
        <p>
        </p><p>当对字符串进行修改的时候，需要使用 StringBuffer 和 StringBuilder 类。
    </p><p>和 String 类不同的是，StringBuffer 和 StringBuilder 类的对象能够被多次的修改，并且不产生新的未使用对象。
    </p><p>StringBuilder 类在 Java 5 中被提出，它和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的（不能同步访问）。
    </p><p>由于 StringBuilder 相较于 StringBuffer 有速度优势，所以多数情况下建议使用 StringBuilder 类。然而在应用程序要求线程安全的情况下，则必须使用 StringBuffer 类。
    </p>
        <div class="example">
            <h2 class="example">Test.java 文件代码：</h2>
            <div class="example_code">
                <div class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Test</span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">StringBuffer</span><span class="hl-code"> </span><span class="hl-identifier">sBuffer</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">StringBuffer</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">菜鸟教程官网：</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">sBuffer</span><span class="hl-code">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">www</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">sBuffer</span><span class="hl-code">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">.runoob</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">sBuffer</span><span class="hl-code">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">.com</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">sBuffer</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></div>
            </div>
        </div>
        <p>以上实例编译运行结果如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">菜鸟教程官网：</span><span class="pln">www</span><span class="pun">.</span><span class="pln">runoob</span><span class="pun">.</span><span class="pln">com</span></pre>
        <h2>StringBuffer 方法</h2>
        <p>以下是 StringBuffer 类支持的主要方法：</p>
        <table class="reference">
            <tbody>
            <tr>
                <th>
                    序号</th>
                <th>
                    方法描述</th>
            </tr>
            <tr>
                <td>
                    1</td>
                <td>
                    public StringBuffer append(String s)<br>
                    将指定的字符串追加到此字符序列。</td>
            </tr>
            <tr>
                <td>
                    2</td>
                <td>
                    public StringBuffer reverse()<br>
                    &nbsp;将此字符序列用其反转形式取代。</td>
            </tr>
            <tr>
                <td>
                    3</td>
                <td>
                    public delete(int start, int end)<br>
                    移除此序列的子字符串中的字符。</td>
            </tr>
            <tr>
                <td>
                    4</td>
                <td>
                    public insert(int offset, int i)<br>
                    将 <code>int</code> 参数的字符串表示形式插入此序列中。</td>
            </tr>
            <tr>
                <td>
                    5</td>
                <td>
                    replace(int start, int end, String str)<br>
                    使用给定 <code>String</code> 中的字符替换此序列的子字符串中的字符。</td>
            </tr>
            </tbody>
        </table>
        <p>下面的列表里的方法和 String 类的方法类似：
        </p>
        <table class="reference">
            <tbody>
            <tr>
                <th style="width:64px;">
                    序号</th>
                <th style="width:517px;">
                    方法描述</th>
            </tr>
            <tr>
                <td style="width:64px;">
                    1</td>
                <td style="width:517px;">
                    int capacity()<br>
                    返回当前容量。</td>
            </tr>
            <tr>
                <td style="width:64px;">
                    2</td>
                <td style="width:517px;">
                    char charAt(int index)<br>
                    返回此序列中指定索引处的 <code>char</code> 值。</td>
            </tr>
            <tr>
                <td style="width:64px;">
                    3</td>
                <td style="width:517px;">
                    void ensureCapacity(int minimumCapacity)<br>
                    确保容量至少等于指定的最小值。</td>
            </tr>
            <tr>
                <td style="width:64px;">
                    4</td>
                <td style="width:517px;">
                    void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)<br>
                    将字符从此序列复制到目标字符数组 <code>dst</code>。</td>
            </tr>
            <tr>
                <td style="width:64px;">
                    5</td>
                <td style="width:517px;">
                    int indexOf(String str)<br>
                    返回第一次出现的指定子字符串在该字符串中的索引。</td>
            </tr>
            <tr>
                <td style="width:64px;">
                    6</td>
                <td style="width:517px;">
                    int indexOf(String str, int fromIndex)<br>
                    从指定的索引处开始，返回第一次出现的指定子字符串在该字符串中的索引。</td>
            </tr>
            <tr>
                <td style="width:64px;">
                    7</td>
                <td style="width:517px;">
                    int lastIndexOf(String str)<br>
                    返回最右边出现的指定子字符串在此字符串中的索引。</td>
            </tr>
            <tr>
                <td style="width:64px;">
                    8</td>
                <td style="width:517px;">
                    int lastIndexOf(String str, int fromIndex)<br>
                    返回 String 对象中子字符串最后出现的位置。</td>
            </tr>
            <tr>
                <td style="width:64px;">
                    9</td>
                <td style="width:517px;">
                    int length()<br>
                    &nbsp;返回长度（字符数）。</td>
            </tr>
            <tr>
                <td style="width:64px;">
                    10</td>
                <td style="width:517px;">
                    void setCharAt(int index, char ch)<br>
                    将给定索引处的字符设置为 <code>ch</code>。</td>
            </tr>
            <tr>
                <td style="width:64px;">
                    11</td>
                <td style="width:517px;">
                    void setLength(int newLength)<br>
                    设置字符序列的长度。</td>
            </tr>
            <tr>
                <td style="width:64px;">
                    12</td>
                <td style="width:517px;">
                    CharSequence subSequence(int start, int end)<br>
                    返回一个新的字符序列，该字符序列是此序列的子序列。</td>
            </tr>
            <tr>
                <td style="width:64px;">
                    13</td>
                <td style="width:517px;">
                    String substring(int start)<br>
                    返回一个新的 <code>String</code>，它包含此字符序列当前所包含的字符子序列。</td>
            </tr>
            <tr>
                <td style="width:64px;">
                    14</td>
                <td style="width:517px;">
                    String substring(int start, int end)<br>
                    返回一个新的 <code>String</code>，它包含此序列当前所包含的字符子序列。</td>
            </tr>
            <tr>
                <td style="width:64px;">
                    15</td>
                <td style="width:517px;">
                    String toString()<br>
                    返回此序列中数据的字符串表示形式。</td>
            </tr>
            </tbody>
        </table>

    </div>
</div>
</body>
</html>
